//
//  MeatMarketApi.m
//  PigMarket
//
//  Created by tunji on 2019/6/13.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "MeatMarketApi.h"
#import "Request.h"
#import "RequestHandler.h"
#import "NetworkConfigration.h"
#import "SystemUtil.h"

//肉市-同城掌柜
#define URLSameCityListBind @"/shopIndex/nearbyManager"
@implementation MeatMarketApi

#pragma mark - 肉市-买肉-广告
- (RACSignal<BuyMeatadvertiseListModel *> *)getBuyMeatAdvertiseListData{

    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLBuyMeatAdvertiseBind];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeNone;
    request.responseType = [BuyMeatadvertiseListModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 肉市-买肉-部位列表
- (RACSignal<BuyMeatPartListModel *> *)getBuyMeatPartListDataWithType:(NSNumber *)type{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLBuyMeatPartBind];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = @{@"type":type};
    request.responseType = [BuyMeatPartListModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 肉市-买肉-店铺列表
- (RACSignal<BuyMeatListModel *> *)getBuyMeatShoppingListDataWithDict:(NSMutableDictionary *)dict{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLBuyMeatListBind];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = dict;
    request.responseType = [BuyMeatListModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 肉市-同城商铺
- (RACSignal<SameCityListModel *> *)getSameCityListDataWithMemberId:(NSNumber *)memberId Page:(NSNumber *)page Limit:(NSNumber *)limit Lon:(NSString *)lon Lat:(NSString *)lat{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLSameCityListBind];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,@"page":page,@"limit":limit,@"lon":lon,@"lat":lat};
    request.responseType = [SameCityListModel class];
    return [self createSignalWithReuqest:request];
}

- (NSString *)getFullURLString:(NSString *)URLString {
    
    NetworkConfigration *configration = [NetworkConfigration defaultNetworkConfiguration];
    return [NSString stringWithFormat:@"%@%@", configration.baseApiURL, URLString];
}



- (RACSignal *)createSignalWithReuqest:(Request *) request {
    
    return [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        RequestHandler *handler = [[RequestHandler alloc] initWithRequest:request responseCallback:^(Response *response) {
            
            
            if (response.code == ResultCodeSuccess) {
                
                if(request.responseType) {
                    [subscriber sendNext:response.data];
                } else {
                    [subscriber sendNext:[NSNull null]];
                }
                [subscriber sendCompleted];
                
            } else {
                if (response.code == ResultCodeDeviceChanged) {//更换设备
                    UserInfo *userInfo = [UserInfoDataProvider sharedInstance].userInfo;
                    userInfo.isLogin = NO;
                    [UserInfoDataProvider sharedInstance].userInfo = userInfo;
                }
                NSError *error = [NSError errorWithDomain:@"com.naturalgrowth.pigmarket.ErrorDomain" code:response.code userInfo: @{ NSLocalizedDescriptionKey : response.msg }];
                [subscriber sendError:error];
            }
            
        }];
        
        [handler sendRequest];
        
        return [RACDisposable disposableWithBlock:^{
            
            
            [handler cancelRequest];
        }];
        
    }] subscribeOn:[RACScheduler scheduler]] ;
}

@end
